<template>
  <el-row v-loading="loading">
    <el-col :span="24">
      <el-row class="search-row">
        <el-col :span="12">
          <el-input placeholder="请输入档案号/姓名" size="small" v-model="like1" style="width: 260px;">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
          <div style="height: 30px;">
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-table :data="findOrder().slice((pageNum-1)*pageSize,pageNum*pageSize)" border size="mini" header-cell-class-name="header-row">
            <el-table-column prop="fileNumber" label="档案号" header-align="center" align="center"></el-table-column>
            <el-table-column prop="name" label="姓名" header-align="center" align="center"></el-table-column>
            <el-table-column prop="sex" label="性别" header-align="center" align="center"></el-table-column>
            <el-table-column prop="age" label="年龄" header-align="center" align="center"></el-table-column>
            <el-table-column prop="phoneNumber" label="手机号" header-align="center" align="center"></el-table-column>
            <el-table-column prop="email" label="邮箱" header-align="center" align="center"></el-table-column>
            <el-table-column prop="createtime" label="建档时间" header-align="center" align="center"></el-table-column>
            <el-table-column label="操作" header-align="center" align="center" width="200px">
              <template slot-scope="scope">
                <el-button type="primary" size="mini"
                  @click="zd(scope.row)">制定计划</el-button>
                  <el-button type="primary" size="mini"
                  @click="zx(scope.row)">执行计划</el-button>
              </template>
            </el-table-column>
        </el-table>
        </el-col>
        <el-col :span="24">
          <el-pagination
            background
            @size-change="handleSizeChange"
            :current-page.sync="pageNum"
            :page-sizes="[2,4,6,8]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="findOrder().length">
          </el-pagination>
          <el-dialog title="干预方案" :visible.sync="dialogTransferVisible">
            <el-tabs type="card">
              <el-tab-pane label="档案信息">
                <h2>信息</h2>
                <table>
                  <tr>
                    <td><span>档案号:</span></td>
                    <td><span v-text="daxx.fileNumber"></span></td>
                    <td></td>
                    <td><span>姓名:</span></td>
                    <td><span v-text="daxx.name"></span></td>
                  </tr>
                  <tr>
                    <td><span>性别:</span></td>
                    <td><span v-text="daxx.sex"></span></td>
                    <td><span>年龄:</span></td>
                    <td><span v-text="daxx.age"></span></td>
                  </tr>
                </table>
                <p>
                  <span>疾病史:</span>
                  <span v-text="daxx.tMemberMedicalhistory.medicalHistory"></span>
                </p>

                 <p>
                  <span>遗传史:</span>
                  <span v-text="daxx.tMemberMedicalhistory.familyHistory"></span>
                </p>

                <p>
                  <span>手术史:</span>
                  <span v-text="daxx.tMemberMedicalhistory.allergicHistory"></span>
                </p>
              </el-tab-pane>

              <el-tab-pane label="体检信息">
                <el-col :span="24">
                  <el-table :data="examinationList.slice((pageNum2-1)*pageSize2,pageNum2*pageSize2)" border size="mini" header-cell-class-name="header-row">
                    <el-table-column prop="tMemberExamination.examDate" label="体检日期" header-align="center" align="center"></el-table-column>
                    <el-table-column prop="name" label="体检项目" header-align="center" align="center"></el-table-column>
                    <el-table-column prop="tMemberData.checkitemData" label="体检结果" header-align="center" align="center"></el-table-column>
                  </el-table>
                </el-col>
                <el-col :span="24">
                  <el-pagination
                    background
                    @size-change="handleSizeChange2"
                    :current-page.sync="pageNum2"
                    :page-sizes="[2,4,6,8]"
                    :page-size="pageSize2"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="examinationList.length">
                  </el-pagination>
                </el-col>
              </el-tab-pane>

              <el-tab-pane label="基本信息">
                <p>
                  <label>档案号:</label>
                  <label v-text="daxx.fileNumber"></label>
                  &emsp;&emsp;&emsp;&emsp;
                  <span>姓名:</span>
                  <span v-text="daxx.name"></span>
                </p>

                 <p>
                  <span>性&emsp;别:</span>
                  <span v-text="daxx.sex"></span>
                  &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&nbsp;
                  <span>年龄:</span>
                  <span v-text="daxx.age"></span>
                </p>

                 <p>
                  <span>健康管理师编号:</span>
                  <span v-text="daxx.healthMgrId"></span>
                </p>
                <p>
                  <span>开始时间:</span>
                  <el-date-picker
                     style="width: 223px;"
                     v-model="scheme.startTime"
                     type="date"
                     placeholder="选择日期"
                     format="yyyy 年 MM 月 dd 日"
                     value-format="yyyy-MM-dd">
                  </el-date-picker>
                </p>
                <p>
                  <span>结束时间:</span>
                  <el-date-picker
                     style="width: 223px;"
                     v-model="scheme.endTime"
                     type="date"
                     placeholder="选择日期"
                     format="yyyy 年 MM 月 dd 日"
                     value-format="yyyy-MM-dd">
                  </el-date-picker>
                </p>
                <p>
                  <span>方案标题:</span>
                  <el-input size="small" v-model="Mb.mbtitle" style="width: 223px;" readonly="readonly"/>
                </p>
                <p>
                  <span>选用模板:</span>
                  <el-select v-model="scheme.modelId" placeholder="请选择" @change="getMB">
                    <el-option
                      v-for="item in qyMubanList"
                      :key="item.mbid"
                      :label="item.mbkey"
                      :value="item.mbid">
                    </el-option>
                  </el-select>
                </p>

                <p>
                  <span>方案目标:</span>
                  <el-input
                    type="textarea"
                    :rows="2"
                    v-model="Mb.fanganmubiao"
                    readonly="readonly">
                  </el-input>
                </p>


              </el-tab-pane>

             <el-tab-pane label="饮食干预">
                <el-table
                    ref="multipleTable"
                    :data="yinshiList.slice((pageNum5-1)*pageSize5,pageNum5*pageSize5)"
                    tooltip-effect="dark"
                    style="width: 100%"
                    @selection-change="handleSelectionChange">
                    <el-table-column
                      type="selection"
                      width="55">
                    </el-table-column>
                    <el-table-column prop="id" label="编号" header-align="center" align="center"></el-table-column>
                    <el-table-column prop="energy" label="推荐摄入热量(g)" header-align="center" align="center"></el-table-column>
                    <el-table-column prop="name" label="推荐推荐饮食" header-align="center" align="center"></el-table-column>
                </el-table>
                <el-pagination
                    background
                    @size-change="handleSizeChange5"
                    :current-page.sync="pageNum5"
                    :page-sizes="[2,4,6,8]"
                    :page-size="pageSize5"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="yinshiList.length">
                </el-pagination>
              </el-tab-pane>
              <el-tab-pane label="运动干预">
                <el-table
                    ref="multipleTable2"
                    :data="yundongList.slice((pageNum3-1)*pageSize3,pageNum3*pageSize3)"
                    tooltip-effect="dark"
                    style="width: 100%"
                    @selection-change="handleSelectionChange2">
                    <el-table-column
                      type="selection"
                      width="55">
                    </el-table-column>
                    <el-table-column prop="id" label="编号" header-align="center" align="center"></el-table-column>
                    <el-table-column prop="movementTime" label="推荐时间(分钟)" header-align="center" align="center"></el-table-column>
                    <el-table-column prop="name" label="推荐运动" header-align="center" align="center"></el-table-column>
                </el-table>
                <el-pagination
                    background
                    @size-change="handleSizeChange3"
                    :current-page.sync="pageNum3"
                    :page-sizes="[2,4,6,8]"
                    :page-size="pageSize3"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="yundongList.length">
                </el-pagination>
              </el-tab-pane>
              <el-tab-pane label="健康方案">
                <el-table
                    ref="multipleTable3"
                    :data="jiankangList.slice((pageNum4-1)*pageSize4,pageNum4*pageSize4)"
                    tooltip-effect="dark"
                    style="width: 100%"
                    @selection-change="handleSelectionChange3">
                    <el-table-column
                      aria-readonly="true"
                      type="selection"
                      width="55">
                    </el-table-column>
                    <el-table-column prop="id" label="编号" header-align="center" align="center"></el-table-column>
                    <el-table-column prop="title" label="标题" header-align="center" align="center"></el-table-column>
                    <el-table-column prop="keywords" label="关键字" header-align="center" align="center"></el-table-column>
                </el-table>
                <el-pagination
                    background
                    @size-change="handleSizeChange4"
                    :current-page.sync="pageNum4"
                    :page-sizes="[2,4,6,8]"
                    :page-size="pageSize4"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="jiankangList.length">
                </el-pagination>
              </el-tab-pane>
            </el-tabs>
            <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="qd()">确 定</el-button>
              <el-button @click="dialogTransferVisible=false">取消</el-button>
            </div>
          </el-dialog>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import {listHy} from '@/api/xsxmember'
import {getBs} from '@/api/xsxmemberMedicalhistory'
import {getMemberExamination} from '@/api/xsxmemberExamination'
import {getQyMuBan,getMB,queryYunDong,queryJianKang,queryYinShi} from '@/api/xsxmuban'
import {getYinShi} from '@/api/xsxmeals'
import {getYunDong} from '@/api/xsxsportsitemlibrary'
import {getJianKang} from '@/api/xsxhealthplan'
import {addScheme,updateStatue,querySchemeByHyid} from '@/api/xsxinterposeScheme'
import { getHealthMgrId} from '@/api/xsxhealthmgr'






export default {
  data() {
      return {
        userId:0,
        multipleSelection: [],
        arr:[],
        multipleSelection2: [],
        arr2:[],
        multipleSelection3: [],
        arr3:[],
        yinshiList:[],
        yundongList:[],
        jiankangList:[],
        qyMubanList:[],
        examinationList:[],
        hyList:[],
        daxx:{hyid:'',fileNumber:"",name:"",sex:"",age:0,healthMgrId:'',tMemberMedicalhistory:{medicalHistory:"55",familyHistory:"55",allergicHistory:"66"}},
        dialogTransferVisible:false,
        classifyList:[],
        loading: false,
        like1:'',
        pageSize:4,//每页显示的条数
        pageNum:1,
        pageSize2:6,//每页显示的条数
        pageNum2:1,
        pageSize5:6,//每页显示的条数
        pageNum5:1,
        pageSize3:6,//每页显示的条数
        pageNum3:1,
        pageSize4:6,//每页显示的条数
        pageNum4:1,
        Mb:{},
        scheme:{
          id:0,
          tMemberId:"",
          schemeTittle:"",
          modelId:"",
          startTime:"",
          endTime:"",
          schemeTarget:"",
          healthMgrId:""
        },
      };
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
     handleSelectionChange2(val) {
      this.multipleSelection2 = val;
    },
     handleSelectionChange3(val) {
      this.multipleSelection3 = val;
    },
     handleSizeChange3(val) {
     this.pageSize3 = val;
    },
    handleSizeChange4(val) {
     this.pageSize4 = val;
    },
     handleSizeChange3(val) {
     this.pageSize3 = val;
    },
    handleSizeChange5(val) {
     this.pageSize5 = val;
    },
    getMB(){
      getMB(this.scheme.modelId).then(response => {
        this.Mb=response.data;
      })
        queryYinShi(this.scheme.modelId).then(response => {
         this.$nextTick(function () {
          let that=this
          that.$refs.multipleTable.clearSelection()
            for(let i=0;i<that.yinshiList.length;i++){
              for(let j=0;j<response.data.length;j++){
                if(that.yinshiList[i].id==response.data[j].yinshiId){
                  that.$refs.multipleTable.toggleRowSelection(that.yinshiList[i],true)
                }
              }
            }
        })
      })

      queryYunDong(this.scheme.modelId).then(response => {
         this.$nextTick(function () {
          let that=this
          that.$refs.multipleTable2.clearSelection()
            for(let i=0;i<that.yundongList.length;i++){
              for(let j=0;j<response.data.length;j++){
                if(that.yundongList[i].id==response.data[j].yundongId){
                  that.$refs.multipleTable2.toggleRowSelection(that.yundongList[i],true)
                }
              }
            }
        })
      })

      queryJianKang(this.scheme.modelId).then(response => {
         this.$nextTick(function () {
          let that=this
          that.$refs.multipleTable3.clearSelection()
            for(let i=0;i<that.jiankangList.length;i++){
              for(let j=0;j<response.data.length;j++){
                if(that.jiankangList[i].id==response.data[j].jiankangId){
                  that.$refs.multipleTable3.toggleRowSelection(that.jiankangList[i],true)
                }
              }
            }
        })
      })
    },
    handleSizeChange(val) {
     this.pageSize = val;
    },
    handleSizeChange2(val) {
     this.pageSize2 = val;
    },
    async pageInit() {
      let userid=JSON.parse(sessionStorage.getItem('currentUser')).id
      try {
        let [ttyy,ys,yd,jk] = await Promise.all([
          getHealthMgrId(userid),
          getYinShi(),
          getYunDong(),
          getJianKang()
        ])
        this.userId=ttyy.data
        this.yinshiList=ys.data
        this.yundongList=yd.data
        this.jiankangList=jk.data
        listHy(this.userId).then(response => {
          this.hyList=response.data
        })
      } catch(error) {
        this.outputError(error)
      }
    },
    findOrder(){
        return this.hyList.filter((item,i)=>{
            if(item.fileNumber.includes(this.like1) || item.name.includes(this.like1)){
                return item;
            }
        })
    },
    zd(row){
      querySchemeByHyid(row.id).then(response => {
        if(response.data.length>0){
          alert("该会员已制定计划，不可重复制定！")
        }else{
           this.daxx.fileNumber=row.fileNumber;
          this.daxx.hyid=row.id;
          this.daxx.name=row.name;
          this.daxx.sex=row.sex;
          this.daxx.age=row.age;
          this.daxx.healthMgrId=row.healthMgrId
          getBs(row.id).then(response => {
            this.daxx.tMemberMedicalhistory=response.data
          })

          getMemberExamination(row.id).then(response => {
            if(response.data!=''){
              this.examinationList=response.data;
            }
          })

          getQyMuBan().then(response => {
            this.qyMubanList=response.data;
          })
          this.dialogTransferVisible=true;
        }
      })

    },
    zx(row){
      this.$confirm('是否确定执行?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          updateStatue(row.id).then(response => {
            this.$message({
              type: 'success',
              message: '执行成功!'
            });
          })
          .catch(error => {
            console.log(error)
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消执行'
          });
        });
    },
    qd(){
      if(this.multipleSelection.length==0){
        alert("请选择饮食干预")
        return;
      }
      if(this.multipleSelection2.length==0){
        alert("请选择运动干预")
        return;
      }
       if(this.multipleSelection3.length==0){
        alert("请选择健康方案")
        return;
      }
      for(let i=0;i<this.multipleSelection.length;i++){
        this.arr.push(this.multipleSelection[i].id)
      }
      for(let i=0;i<this.multipleSelection2.length;i++){
        this.arr2.push(this.multipleSelection2[i].id)
      }
      for(let i=0;i<this.multipleSelection3.length;i++){
        this.arr3.push(this.multipleSelection3[i].id)
      }
      this.scheme.tMemberId=this.daxx.hyid;
      this.scheme.schemeTittle=this.Mb.mbtitle;
      this.scheme.schemeTarget=this.Mb.fanganmubiao;
      this.scheme.healthMgrId=this.daxx.healthMgrId;
      addScheme(this.scheme).then(response => {
        if(response.data>0){
          alert("制定计划成功")
          this.pageInit()
        }
      })

      this.dialogTransferVisible=false;
    },
  },
  created() {
    this.pageInit()
  }
}
</script>
